from django.shortcuts import render_to_response
from django.template import Template, Context
from django.template.loader import get_template
from django.http import HttpResponse
from computer.models import *

def main(request):  
    Computer = DetailComperterPerfect.objects.all()
    t = get_template('main.html')
    html = t.render(Context({'Computers': Computer}))
    return HttpResponse(html)

def report_computer(request):
    Computer = DetailComperterPerfect.objects.all()
    t = get_template('reportComputer.html')
    html = t.render(Context({'Computers': Computer}))
    return HttpResponse(html)

def report_computer_damaged(request):
    Computer = DetailComperterDamaged.objects.all()
    t = get_template('reportComputerDamaged.html')
    html = t.render(Context({'Computers': Computer}))
    return HttpResponse(html)

def report_computer_svg(request):
    f = open("/home/naynam/s53_1156_509_6/report_svg.svg", "r+")
    f.write("<?xml version=\"1.0\" standalone=\"no\"?>")
    f.write("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">")
    f.write("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">")
    f.write("<text x=\"20\" y=\"50\" fill=\"black\"> NUMBER </text>\
	<text x=\"20\" y=\"600\" fill=\"black\"> 00 _</text>\
  	<text x=\"20\" y=\"550\" fill=\"black\"> 05 _</text>\
	<text x=\"20\" y=\"500\" fill=\"black\"> 10 _</text>\
	<text x=\"20\" y=\"450\" fill=\"black\"> 15 _</text>\
	<text x=\"20\" y=\"400\" fill=\"black\"> 20 _</text>\
	<text x=\"20\" y=\"350\" fill=\"black\"> 25 _</text>\
	<text x=\"20\" y=\"300\" fill=\"black\"> 30 _</text>\
	<text x=\"20\" y=\"250\" fill=\"black\"> 35 _</text>\
	<text x=\"20\" y=\"200\" fill=\"black\"> 40 _</text>\
	<text x=\"20\" y=\"150\" fill=\"black\"> 45 _</text>\
	<text x=\"20\" y=\"100\" fill=\"black\"> 50 _</text>")

    f.write("<line x1=\"49\" y1=\"70\" x2=\"49\" y2=\"600\"\
	     style=\"stroke:black;stroke-width:2\"/>")

    f.write("<line x1=\"48\" y1=\"601\" x2=\"400\" y2=\"600\"\
	     style=\"stroke:black;stroke-width:2\"/>")

    f.write("<text x=\"420\" y=\"600\" fill=\"black\"> STATUS </text>")

    f.write("<text x=\"450\" y=\"115\" fill=\"black\"> Computer is operative</text>") 
    f.write("<text x=\"450\" y=\"165\" fill=\"black\"> Computer is break down</text>") 

    f.write("<rect x=\"400\" y=\"100\" width=\"25\" height=\"25\"\
	     style=\"fill:green;stroke-width:5; stroke-opacity:0.9\"/>")
    f.write("<rect x=\"400\" y=\"150\" width=\"25\" height=\"25\"\
	     style=\"fill:red;stroke-width:5; stroke-opacity:0.9\"/>")
    i = 0
    j = 0
    ComForUse = DetailComperterPerfect.objects.all()
    ComForRepair = DetailComperterDamaged.objects.all()
    	
    for ComForUse in ComForUse :
        i = i + 1;
    for ComForRepair in ComForRepair :
        j = j + 1;
    com_for_use = str((i - j) * 10)
    com_for_repair = str(j * 10)
    
    f.write("<rect x=\"130\" y=\""+ str(600 -int(com_for_use)) +
	    "\" width=\"50\" height= \"" + com_for_use + "\"\
	    style=\"fill:green;stroke:none;stroke-width:5;opacity:0.9\"/>" )
    f.write("<text x=\"150\" y=\""+ str(590 -int(com_for_use)) +
	     "\" fill=\"black\">"+ str(i-j)+ "</text>")

    f.write("<rect x=\"260\" y=\""+ str(600 -int(com_for_repair)) +
	    "\" width=\"50\" height=\""+ com_for_repair +"\"\
            style=\"fill:red;stroke:none;stroke-width:5;opacity:0.9\"/>" )
    f.write("<text x=\"280\" y=\""+ str(590 -int(com_for_repair)) +
	     "\" fill=\"black\">"+ str(j)+ "</text>")
    f.write("</svg>") 
    f.write("<center><form action=\"http://127.0.0.1:8000/\" method=\"get\"><input type=\"submit\" value=\"Back to main page\"></form></center>")   
    
    f.closed
        
    return render_to_response('report_svg.svg')


